/*
 * StreamToString.java
 * This file is part of the <a href="http://code.google.com/p/pux">PUX Framework</a>
 *
 * Copyright 2008 by <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package de.teamskill.pux.library.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import de.teamskill.pux.engine.AbstractNode;
import de.teamskill.pux.engine.ConfigurationPort;
import de.teamskill.pux.engine.InputPort;
import de.teamskill.pux.engine.OutputPort;

/**
 * A StreamToString node reads from a (textual) InputStream and provides all
 * data read in a string as output
 * 
 * @author <a href="mailto:jfendler@polytechnic.edu.na">Jens Fendler</a>
 * 
 */
public class StreamToString extends AbstractNode {

	/**
	 * IN_STREAM
	 */
	public static final String IN_STREAM = "STREAM";

	/**
	 * OUT_STRING
	 */
	public static final String OUT_STRING = "STRING";

	/**
	 * CONFIG_LINEFEED
	 */
	public static final String CONFIG_LINEFEED = "lineFeed";

	/**
	 * in
	 */
	private InputPort in;

	/**
	 * out
	 */
	private OutputPort out;

	/**
	 * lf
	 */
	private ConfigurationPort lf;

	/**
	 * StreamToString Constructor
	 * 
	 */
	public StreamToString() {
		this("StreamToString" + nr++, false);
	}

	/**
	 * StreamToString Constructor
	 * 
	 * @param nodeName
	 * @param continuous
	 */
	public StreamToString(String nodeName, boolean continuous) {
		super(nodeName, continuous);
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#compute()
	 */
	public void compute() {
		BufferedReader br = new BufferedReader(new InputStreamReader((InputStream) in.fetchData()));
		StringBuffer sb = new StringBuffer();
		String lfSequence = System.getProperty("line.separator");
		boolean lineFeed = ((Boolean) lf.fetchData()).booleanValue();
		String line = null;
		try {
			while ((line = br.readLine()) != null) {
				sb.append(line);
				if (lineFeed) {
					sb.append(lfSequence);
				}
			}
		} catch (IOException e) {
			throw new IllegalStateException(e);
		} finally {
			try {
				br.close();
			} catch (Exception e) {
			}
		}
		out.pushData(sb.toString());
	}

	/**
	 * @see de.teamskill.pux.engine.AbstractNode#createPorts()
	 */
	protected void createPorts() {
		in = addInputPort(IN_STREAM, InputStream.class, true);
		out = addOutputPort(OUT_STRING, String.class);
		lf = addConfigurationPort(CONFIG_LINEFEED, Boolean.class, new Boolean(true));
	}

	/**
	 * JavaBean-style getter-method for in
	 * 
	 * @return the in
	 */
	public InputPort getIn() {
		return in;
	}

	/**
	 * /** JavaBean-style setter-method for in
	 * 
	 * @param in
	 *            the in to set
	 */
	public void setIn(InputPort in) {
		this.in = in;
	}

	/**
	 * JavaBean-style getter-method for out
	 * 
	 * @return the out
	 */
	public OutputPort getOut() {
		return out;
	}

	/**
	 * /** JavaBean-style setter-method for out
	 * 
	 * @param out
	 *            the out to set
	 */
	public void setOut(OutputPort out) {
		this.out = out;
	}

	/**
	 * JavaBean-style getter-method for lf
	 * 
	 * @return the lf
	 */
	public ConfigurationPort getLf() {
		return lf;
	}

	/**
	 * /** JavaBean-style setter-method for lf
	 * 
	 * @param lf
	 *            the lf to set
	 */
	public void setLf(ConfigurationPort lf) {
		this.lf = lf;
	}

}
